//
//  sureView.m
//  LubanWork
//
//  Created by xiaotong on 2020/1/2.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "sureView.h"


@interface sureView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic,strong)UIButton *sureBtn;

@property (nonatomic,strong)UIButton *cancelBtn;

@property (nonatomic,strong)UITableView *tableview;

@property (nonatomic,strong)NSArray *section;

@property (nonatomic,strong)UIView *backView;

@property (nonatomic,copy)void(^isOrNo)(BOOL isOrNo) ;

@end


@implementation sureView

-(instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = kColorFromARGBHex(0x000000,0.5);
        self.frame = [UIScreen mainScreen].bounds;
        UIView *backView = [[UIView alloc] init];
        [self addSubview:backView];
        _backView = backView;
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-BOTTOM_HEIGHT);
            make.left.right.equalTo(self);
            make.height.mas_offset(kSCRATIO(200));
        }];
        
        backView.backgroundColor = ColorWhite;
        
        _sureBtn = [UIButton CreatButtontext:@"立即报名" image:nil Font:kFONT(17) Textcolor:kColorFromRGBHex(0xFFFFFF)];
        [backView addSubview:_sureBtn];
        
        
        _sureBtn.backgroundColor = kColorFromRGBHex(0x775FFB);
        
        
        [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(backView.mas_bottom);
            make.right.equalTo(backView.mas_right);
            make.height.mas_offset(kSCRATIO(50));
            make.width.mas_offset(WIDTH/2);
        }];
        
        
        
        _sureBtn.layer.shadowColor = [UIColor colorWithRed:1/255.0 green:6/255.0 blue:29/255.0 alpha:0.1].CGColor;
        _sureBtn.layer.shadowOffset = CGSizeMake(0,0);
        _sureBtn.layer.shadowOpacity = 1;
        _sureBtn.layer.shadowRadius = 5;
        
        
        [_sureBtn addTarget:self action:@selector(isChick:) forControlEvents:UIControlEventTouchUpInside];
        _section = @[@[@"确认工作时间",@"确认工作地点"],@[@"空闲时间即可",@"在家可做"]];
        [backView addSubview:self.tableview];
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_sureBtn.mas_top);
            make.left.right.equalTo(self);
            make.height.mas_offset(kSCRATIO(120));
        }];
        
        
        _cancelBtn = [UIButton CreatButtontext:@"取消" image:nil Font:kFONT(17) Textcolor:kColorFromRGBHex(0xCCCCCC)];
        [backView addSubview:_cancelBtn];
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(backView.mas_bottom);
            make.left.equalTo(backView);
            make.height.mas_offset(kSCRATIO(50));
            make.width.mas_offset(WIDTH/2);
        }];
        [_cancelBtn addTarget:self action:@selector(isChick:) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.backgroundColor = ColorWhite;
        _cancelBtn.layer.shadowColor = [UIColor colorWithRed:1/255.0 green:6/255.0 blue:29/255.0 alpha:0.1].CGColor;
        _cancelBtn.layer.shadowOffset = CGSizeMake(0,0);
        _cancelBtn.layer.shadowOpacity = 1;
        _cancelBtn.layer.shadowRadius = 5;
        
        
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, kSCRATIO(30))];
//        [backView addSubview:view];
        UILabel *titleLab = [UILabel CreatLabeltext:@"*小鲁班提醒您：正规职位报名不会收取任何费用，请您提高警惕哦" Font:kFONT(11) Textcolor:kColorFromRGBHex(0x775FFB) textAlignment:NSTextAlignmentCenter];
        titleLab.frame = CGRectMake(0, 0, WIDTH, kSCRATIO(30));
        [backView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(backView);
            make.left.right.equalTo(backView);
            make.height.mas_offset(kSCRATIO(30));
        }];
        
        titleLab.backgroundColor = ColorWhite;
        titleLab.layer.shadowColor = [UIColor colorWithRed:1/255.0 green:6/255.0 blue:29/255.0 alpha:0.1].CGColor;
        titleLab.layer.shadowOffset = CGSizeMake(0,0);
        titleLab.layer.shadowOpacity = 1;
        titleLab.layer.shadowRadius = 5;;
        
        
        @weakify(self);
        UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weak_self dismiss];
            if (weak_self.isOrNo) {
                weak_self.isOrNo(NO);
            }
        }];
        
        gest.delegate = self;
        [self addGestureRecognizer:gest];
    }
    return self;
}

-(void)isChick:(UIButton *)sender{
    BOOL is = NO;
    if (sender == _cancelBtn) {
        is = NO;
    }
    if (sender == _sureBtn) {
        is = YES;
    }
    
    if (self.isOrNo) {
        self.isOrNo(is);
    }
    [self dismiss];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        UILabel *labe = [UILabel CreatLabeltext:_section[0][indexPath.row] Font:kFONT(14) Textcolor:kColorFromRGBHex(0x666666) textAlignment:NSTextAlignmentLeft];
        [cell.contentView addSubview:labe];
        [labe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.contentView).offset(kSCRATIO(14));
            make.left.equalTo(cell.contentView).offset(kSCRATIO(25));
            make.right.equalTo(cell.contentView).offset(-kSCRATIO(25));
            make.height.mas_offset(kSCRATIO(13.5));
        }];
        
        UILabel *detailLab = [UILabel CreatLabeltext:_section[1][indexPath.row] Font:kFONT(12) Textcolor:kColorFromRGBHex(0x666666) textAlignment:NSTextAlignmentLeft];
        detailLab.text = self.arr[indexPath.row];
        [cell.contentView addSubview:detailLab];
        [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(cell.contentView.mas_bottom).offset(-kSCRATIO(14));
            make.left.equalTo(cell.contentView).offset(kSCRATIO(25));
            make.right.equalTo(cell.contentView.mas_right).offset(-kSCRATIO(25));
            make.height.mas_offset(kSCRATIO(12));
            
        }];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kSCRATIO(60);
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, kSCRATIO(120)) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.scrollEnabled = NO;
    }
    return _tableview;
}



-(void)show:(void (^)(BOOL))isOrNo{
    _isOrNo = isOrNo;
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

-(void)dismiss{
    [self removeAllSubviews];
    [self removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceiveTouch:(UITouch *)touch {

    if ([self.backView pointInside:[touch locationInView:self.backView] withEvent:nil]) {
        return NO;
    }
    
    return YES;
}

@end
